package com.tfswx.jcyw.dmzc.util;

import lombok.SneakyThrows;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张述江
 * @date 2023/6/2
 */
public class PinyinUtil {
    private static final HanyuPinyinOutputFormat format;

    static {
        //输出格式设置
        format = new HanyuPinyinOutputFormat();
        /**
         * 输出大小写设置
         *
         * LOWERCASE:输出小写
         * UPPERCASE:输出大写
         */
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

        /**
         * 输出音标设置
         *
         * WITH_TONE_MARK:直接用音标符（必须设置WITH_U_UNICODE，否则会抛出异常）
         * WITH_TONE_NUMBER：1-4数字表示音标
         * WITHOUT_TONE：没有音标
         */
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        /**
         * 特殊音标ü设置
         *
         * WITH_V：用v表示ü
         * WITH_U_AND_COLON：用"u:"表示ü
         * WITH_U_UNICODE：直接用ü
         */
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    }

    @SneakyThrows
    public static List<String> getPinyin(@NotNull String hz) {
        char[] bytes = hz.trim().toCharArray();

        List<List<String>> wrods = new ArrayList<>();
        for (char aByte : bytes) {
            //匹配是否是汉字
            if (Character.toString(aByte).matches("[\\u4E00-\\u9FA5]+")) {
                //如果是多音字，返回多个拼音
                String[] pys = PinyinHelper.toHanyuPinyinStringArray(aByte, format);
                wrods.add(Arrays.stream(pys).collect(Collectors.toList()));
            } else {
                wrods.add(Collections.singletonList(Character.toString(aByte)));
            }
        }
        int max = 1;
        for (List<String> wrod : wrods) {
            max *= wrod.size();
        }
        String[] py = new String[max];
        Arrays.fill(py, "");
        for (List<String> wrod : wrods) {
            for (int i = 0; i < py.length; i++) {
                py[i] += wrod.get(i % wrod.size()).toLowerCase();
            }
        }

        return Arrays.stream(py).distinct().collect(Collectors.toList());
    }

    @SneakyThrows
    public static List<String> getFirstLetter(String hz) {
        char[] bytes = hz.trim().toCharArray();

        List<List<String>> wrods = new ArrayList<>();
        for (char aByte : bytes) {
            //匹配是否是汉字
            if (Character.toString(aByte).matches("[\\u4E00-\\u9FA5]+")) {
                //如果是多音字，返回多个拼音
                String[] pys = PinyinHelper.toHanyuPinyinStringArray(aByte, format);
                wrods.add(Arrays.stream(pys).map(item -> String.valueOf(item.charAt(0))).collect(Collectors.toList()));
            } else {
                wrods.add(Collections.singletonList(Character.toString(aByte)));
            }
        }
        int max = 1;
        for (List<String> wrod : wrods) {
            max *= wrod.size();
        }
        String[] py = new String[max];
        Arrays.fill(py, "");
        for (List<String> wrod : wrods) {
            for (int i = 0; i < py.length; i++) {
                py[i] += wrod.get(i % wrod.size()).toLowerCase();
            }
        }

        return Arrays.stream(py).distinct().collect(Collectors.toList());
    }

    // 1   1,2    1,2,3    1
    // 1111 1221 1131 1211 1121 1231
}
